﻿using BBCore.Core.Models.Admins;
using BBCore.Core.Models.BaseModels;
using BBCore.Core.Models.EnumTypes;
using BBCore.Infrastructure.Helps;
using BBCore.Server.Components.Pages;

namespace BBCore.Server.Seed
{
    /// <summary>
    /// 种子数据
    /// </summary>
    public static class DataInitializer
    {
        /// <summary>
        /// 种子数据初始化
        /// </summary>
        /// <param name="services"></param>
        /// <param name="FreeSql"></param>
        /// <returns></returns>
        public static IServiceCollection AddDb(this IServiceCollection services, IFreeSql FreeSql)
        {
            if (!UserEntity.Where(x => x.UserName == BBCore_Constant.adminName).Any())
            {
                // 插入角色数据
                var roleItems = new List<RoleEntity>
                {
                     new RoleEntity{  Id=1, RoleName="管理员", CreateTime=DateTime.Now, IsDeleted=false,Sort=1 },
                     new RoleEntity{  Id=2, RoleName="普通用户", CreateTime=DateTime.Now, IsDeleted=false,Sort=2 },
                };
                RoleEntity.Orm.Insert(roleItems).ExecuteAffrows();
                //插入用户数据
                var userItems = new List<UserEntity>
                {
                    new UserEntity{  Id=1,UserName=BBCore_Constant.adminName,Password=MD5Encryption.Encrypt(BBCore_Constant.adminName), NickName="管理员", RoleId=1,CreateTime=DateTime.Now,IsDeleted=false,Sort=1},
                    new UserEntity{  Id=2,UserName="user",Password=MD5Encryption.Encrypt("user"), NickName="普通用户", RoleId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=2}
                };
                UserEntity.Orm.Insert(userItems).ExecuteAffrows();
                //插入菜单数据
                var menuItems = new List<MenuEntity>
                {
                    new MenuEntity{ Id=1,MenuName="首页",Url="/",Icon="fa fa-home",ParentId=0,CreateTime=DateTime.Now,IsDeleted=false,Sort=1},
                    new MenuEntity{ Id=2,MenuName="管理页面",Url="/",Icon="fa fa-fw fa-database",ParentId=0,CreateTime=DateTime.Now,IsDeleted=false,Sort=2},
                    new MenuEntity{ Id=3,MenuName="用户管理",Url="/user",Icon="fa fa-user",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=3},
                    new MenuEntity{ Id=4,MenuName="角色管理",Url="/role",Icon="fa fa-users",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=4},
                    new MenuEntity{ Id=5,MenuName="菜单管理",Url="/menu",Icon="far fa-bars",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=5},
                    new MenuEntity{ Id=6,MenuName="按钮权限",Url="/buttonpermission",Icon="fas fa-bowl-food",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=6},
                    new MenuEntity{ Id=7,MenuName="字典管理",Url="/dictmanager",Icon="fas fa-box-archive",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=7},
                    new MenuEntity{ Id=8,MenuName="公告栏管理",Url="/AnnouncementPage",Icon="fas fa-bottle-droplet",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=8},
                    new MenuEntity{ Id=9,MenuName="日志查看",Url="/logsviewer",Icon="fas fa-fish-fins",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=9},
                    new MenuEntity{ Id=10,MenuName="记录锁管理",Url="/RecordLock_management",Icon="fas fa-hands-praying",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=10},
                    new MenuEntity{ Id=11,MenuName="单表CRUD代码生成器",Url="/Single_Table_Generate",Icon="fas fa-arrows-split-up-and-left",ParentId=2,CreateTime=DateTime.Now,IsDeleted=false,Sort=11},
                    new MenuEntity{ Id=12,MenuName="示例",Url="/",Icon="fas fa-arrows-left-right-to-line",ParentId=0,CreateTime=DateTime.Now,IsDeleted=false,Sort=12},
                    new MenuEntity{ Id=13,MenuName="动态Sql查询",Url="/",Icon="fas fa-shield-dog",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=13},
                    new MenuEntity{ Id=14,MenuName="单一Sql查询",Url="/Samples/SingleSqlToExcel/14", Query_Sql="select * from hyj_user",Icon="fas fa-hands-praying",ParentId=13,CreateTime=DateTime.Now,IsDeleted=false,Sort=14},
                    new MenuEntity{ Id=15,MenuName="带参Sql查询",Url="/Samples/Dynamic_Content/15", CN_COLUMNS="用户名,显示名", Query_Sql="select username,nickname from hyj_user where username=请输入用户名",Icon="fas fa-hands-praying",ParentId=13,CreateTime=DateTime.Now,IsDeleted=false,Sort=15},
                    new MenuEntity{ Id=16,MenuName="Timer定时刷新示例",Url="/samples/TimerRefresh",Icon="fas fa-hands-praying",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=16},
                    new MenuEntity{ Id=17,MenuName="获取焦点",Url="/samples/focus",Icon="fas fa-hands-praying",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=17},
                    new MenuEntity{ Id=18,MenuName="商品秒杀模拟",Url="/Seckill",Icon="fas fa-hands-praying",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=18},
                    new MenuEntity{ Id=19,MenuName="BKC主从明细",Url="/Samples/MasterDetail",Icon="fas fa-hands-praying",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=19},
                    new MenuEntity{ Id=20,MenuName="单表CRUD",Url="/HYJ_Single_Table_CRUD",Icon="fas fa-hands-praying",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=20},
                    new MenuEntity{ Id=21,MenuName="主从明细",Url="/HYJ_MasterDetail_Table_CRUD",Icon="fas fa-hands-praying",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=21},
                    new MenuEntity{ Id=22,MenuName="弹窗主从明细",Url="/HYJ_MasterDetail_Modal",Icon="fas fa-hands-praying",ParentId=12,CreateTime=DateTime.Now,IsDeleted=false,Sort=22}

                };
                MenuEntity.Orm.Insert(menuItems).ExecuteAffrows();
                //插入角色菜单关系表数据
                var rolemenuItems = new List<RoleMenuEntity>
                {
                    //管理员
                    new RoleMenuEntity{ RoleId=1, MenuId=1},
                    new RoleMenuEntity{ RoleId=1, MenuId=2},
                    new RoleMenuEntity{ RoleId=1, MenuId=3},
                    new RoleMenuEntity{ RoleId=1, MenuId=4},
                    new RoleMenuEntity{ RoleId=1, MenuId=5},
                    new RoleMenuEntity{ RoleId=1, MenuId=6},
                    new RoleMenuEntity{ RoleId=1, MenuId=7},
                    new RoleMenuEntity{ RoleId=1, MenuId=8},
                    new RoleMenuEntity{ RoleId=1, MenuId=9},
                    new RoleMenuEntity{ RoleId=1, MenuId=10},
                    new RoleMenuEntity{ RoleId=1, MenuId=11},
                    new RoleMenuEntity{ RoleId=1, MenuId=12},
                    new RoleMenuEntity{ RoleId=1, MenuId=13},
                    new RoleMenuEntity{ RoleId=1, MenuId=14},
                    new RoleMenuEntity{ RoleId=1, MenuId=15},
                    new RoleMenuEntity{ RoleId=1, MenuId=16},
                    new RoleMenuEntity{ RoleId=1, MenuId=17},
                    new RoleMenuEntity{ RoleId=1, MenuId=18},
                    new RoleMenuEntity{ RoleId=1, MenuId=19},
                    new RoleMenuEntity{ RoleId=1, MenuId=20},
                    new RoleMenuEntity{ RoleId=1, MenuId=21},
                    new RoleMenuEntity{ RoleId=1, MenuId=22},
                    //普通用户
                    new RoleMenuEntity{ RoleId=2, MenuId=12},
                    new RoleMenuEntity{ RoleId=2, MenuId=17},

                };
                FreeSql.Insert(rolemenuItems).ExecuteAffrows();
                //插入带参sql查询示例数据
                var controlDetailsItems = new List<ControlDetails>
                {
                    new ControlDetails{ Id=1, Menu_AuthId=15, Label="请输入用户名", Type=ControlTypeEnum.TextEdit,IsRequired=true, ORDERBY=1 }                    
                };
                FreeSql.Insert(controlDetailsItems).ExecuteAffrows();
            }
            return services;

        }
    }
}
